package net.jxiny.oschina.synthetical.fragment;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;


import com.okhttplib.HttpInfo;
import com.okhttplib.OkHttpUtil;
import com.okhttplib.callback.Callback;

import net.jxiny.oschina.R;
import net.jxiny.oschina.net.URLList;
import net.jxiny.oschina.synthetical.Adapter.AnswerAdapter;
import net.jxiny.oschina.synthetical.Result.AnswerResult;
import net.jxiny.oschina.synthetical.enyity.Answer;
import net.jxiny.oschina.util.ACache;


import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;

public class AnswerFragemnt extends Fragment {
    private Unbinder binder;
    @BindView(R.id.recyclerView_answer)
    RecyclerView AnswerView;
    private List<Answer> lists = new ArrayList<>();
    private AnswerAdapter mAnswerAdapter;

    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_answer, container, false);
        binder = ButterKnife.bind(this, view);
        return view;
    }


    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        Answer answer = new Answer();
        OkHttpUtil.getDefault(this)
                .doGetAsync(HttpInfo.Builder().setUrl(URLList.GET_ANSWER +
                        ACache.get(getActivity()).getAsString("token")+"&catalog=1").build(), new Callback() {
                    @Override
                    public void onSuccess(HttpInfo info) throws IOException {

                        AnswerResult result = info.getRetDetail(AnswerResult.class);
                        mAnswerAdapter.replaceData(result.getPost_list());
                    }
                    @Override
                    public void onFailure(HttpInfo info) throws IOException {
                        Toast.makeText(getContext(),"发送失败"+info,Toast.LENGTH_LONG).show();

                    }
                });
        //*********************************************
        RecyclerView.LayoutManager manager = new LinearLayoutManager(getActivity(),RecyclerView.VERTICAL,false);
        AnswerView.setLayoutManager(manager);
        mAnswerAdapter = new AnswerAdapter(R.layout.item_answer,lists);
        AnswerView.setAdapter(mAnswerAdapter);
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        binder.unbind();
    }
}
